package alfred.proctice.com.recycleview;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by self on 2017/8/31.
 */

public class TimeUtil {
    public final static String SIMPLEFORMATTYPESTRING5 = "HH:mm";

    public final static String SIMPLEFORMATTYPESTRING6 = "yyyy-MM";

    public final static String SIMPLEFORMATTYPESTRING7 = "yyyy-MM-dd";

    public final static String SIMPLEFORMATTYPESTRING8 = "MM-dd";

    public final static String SIMPLEFORMATTYPESTRING9 = "yyyy-MM-dd HH:mm";

    public final static String SIMPLEFORMATTYPESTRING10 = "MM-dd HH:mm";


    /**
     * 把long 转换成 日期 再转换成String类型
     */
    public static String transferLongToDate(String dateFormat, Long millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Date date = new Date(millSec);
        return sdf.format(date);
    }

    public static String getDateStr(Date date, String strFormat) {
        SimpleDateFormat format = new SimpleDateFormat(strFormat);
        return format.format(date);
    }

    /**
     * @param strDate
     * @return
     */
    public static String getSplitMonthDay(String strDate) {
        String[] split = strDate.split("-");
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(split[1] + "月");
        stringBuffer.append(split[2] + "日");
        return stringBuffer.toString();
    }

    //获取两个时间的时间差:多少年多少月
    public static String getTimeap(String startTime, String endTime) {
        if (startTime.length() < 7) {
            startTime.replace("-", "-0");
        }
        SimpleDateFormat sdf = new SimpleDateFormat(SIMPLEFORMATTYPESTRING6);
        try {
            Date startDate = sdf.parse(startTime);
            Date endDate = TextUtils.isEmpty(endTime) || "至今".equals(endTime) ? new Date() : sdf.parse(endTime);
            long gap = endDate.getTime() - startDate.getTime();
            long yearMilliseconds = 365 * 24 * 60 * 60 * 1000L;
            long monthMilliseconds = 30 * 24 * 60 * 60 * 1000L;
            int year = (int) (gap / yearMilliseconds);
            int month = (int) Math.ceil(gap % yearMilliseconds / monthMilliseconds);
            if (year == 0) {
                return month + "个月";
            }
            if (month == 0) {
                return year + "年";
            }
            return year + "年" + month + "个月";
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return "";
    }

}
